package com.linkhub.oms.admin.modules.order.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.linkhub.oms.admin.modules.order.domain.param.OrderLogisticParam;
import com.linkhub.oms.admin.modules.order.domain.vo.LogisticFreightVO;
import com.linkhub.oms.admin.modules.order.domain.vo.OrderLogisticVO;
import com.linkhub.oms.admin.modules.order.domain.vo.orderpdf.OrderPdfVO;
import com.linkhub.oms.admin.modules.order.service.OrderBizService;
import com.linkhub.oms.admin.modules.order.service.OrderInfoAssembleService;
import com.linkhub.oms.common.constant.RoleConstant;
import com.linkhub.oms.common.response.DataResponse;
import com.linkhub.oms.component.freemarker.util.PdfUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @Description: OrderController
 * @Author: echo
 * @Date: 2024/5/25 10:23
 * @Version: 1.0
 */
@Slf4j
@RestController
@Api(tags = {"通用订单接口模块"})
@RequestMapping("/order")
@SaCheckLogin
public class OrderController {

    @Resource
    private OrderBizService orderBizService;
    @Resource
    private OrderInfoAssembleService orderInfoAssembleService;
    @Resource
    private FreeMarkerConfigurer freeMarkerConfiguer;

    private static final String PDF_TEMPLATE = "order.ftl";
    private static final String PDF_NAME = "装箱单.pdf";

    @ApiOperation("查询订单可选的物流列表接口")
    @GetMapping("/logistics/{sysOrderId}")
    @SaCheckRole(value = {RoleConstant.CUSTOMER, RoleConstant.EMPLOYEE}, mode = SaMode.OR)
    public DataResponse<List<OrderLogisticVO>> listOrderLogistics(@PathVariable("sysOrderId") String sysOrderId) {
        return DataResponse.success(orderBizService.listOrderLogistics(sysOrderId));
    }


    @ApiOperation("查询物流渠道运费接口")
    @GetMapping("/estimated_freight/{sysOrderId}")
    public DataResponse<LogisticFreightVO> queryLogisticFreight(@PathVariable("sysOrderId") String sysOrderId,
                                                                @RequestParam("channelCode") String channelCode) {
        return DataResponse.success(orderBizService.queryLogisticFreight(sysOrderId, channelCode));
    }


    @ApiOperation("修改订单物流渠道接口")
    @PostMapping("/logistics/{sysOrderId}")
    public DataResponse<Void> updateOrderLogistic(@PathVariable("sysOrderId") String sysOrderId,
                                                  @Valid @RequestBody OrderLogisticParam logisticParam) {

        logisticParam.setSysOrderId(Lists.newArrayList(sysOrderId));
        orderBizService.updateOrderLogistic(logisticParam);
        return DataResponse.success();
    }

    @ApiOperation("拦截运单接口")
    @GetMapping("/logistics/intercept/{sysOrderId}")
    @SaCheckRole(value = {RoleConstant.CUSTOMER, RoleConstant.EMPLOYEE}, mode = SaMode.OR)
    public DataResponse<Void> interceptLogistics(@PathVariable("sysOrderId") String sysOrderId) {
        return DataResponse.success(orderBizService.interceptLogistics(sysOrderId));
    }

    @ApiOperation("装箱单预览")
    @GetMapping("/pdf/preview/{sysOrderId}")
    @SaIgnore
    public DataResponse<Void> pdfPreview(HttpServletResponse response, @PathVariable("sysOrderId") String sysOrderId) {
        List<OrderPdfVO> orderPdfVOs = orderInfoAssembleService.assembleOrderPdfInfos(Lists.newArrayList(sysOrderId));
        if(CollUtil.isNotEmpty(orderPdfVOs)) {
            Map<String, Object> variables = Maps.newHashMap();
            variables.put("order", orderPdfVOs.get(0));
            List<Map<String, Object>> listVars = Lists.newArrayList(variables);
            PdfUtil.preview(freeMarkerConfiguer, PDF_TEMPLATE, listVars, response);
        }
        return DataResponse.success();
    }

    @ApiOperation("装箱单下载")
    @GetMapping("/pdf/download/{sysOrderId}")
    @SaIgnore
    public void pdfDownload(HttpServletResponse response, @PathVariable("sysOrderId") String sysOrderId) {
        List<OrderPdfVO> orderPdfVOs = orderInfoAssembleService.assembleOrderPdfInfos(Lists.newArrayList(sysOrderId));
        if(CollUtil.isNotEmpty(orderPdfVOs)) {
            Map<String, Object> variables = Maps.newHashMap();
            variables.put("order", orderPdfVOs.get(0));
            List<Map<String, Object>> listVars = Lists.newArrayList(variables);
            PdfUtil.download(freeMarkerConfiguer, PDF_TEMPLATE, listVars, response, PDF_NAME);
        }
    }

}
